package com.example.xiaodong.cn.dajiqi;

import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.xiaodong.cn.dajiqi.data.Gongutils;
import com.example.xiaodong.cn.dajiqi.data.JavaBean;
import com.example.xiaodong.cn.dajiqi.data.TextAdapter;
import com.example.xiaodong.cn.dajiqi.uilts.ImageDialog;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private ListView listView;
    private EditText te_input;
    private Button te_bu;
    private TextAdapter adapter;
    private ArrayList<JavaBean> list;
   private RequestQueue mQueue;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        intiView();
        setClickListener();
    }

    @Override
    protected void onStart() {
        super.onStart();
       xiaoXi();
    }

    /**
     * 发送消息
     */
    private void xiaoXi() {
        te_bu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String string = te_input.getText().toString();
                if (string.equals("")){
                    Toast.makeText(MainActivity.this,"不能为空哦",Toast.LENGTH_LONG).show();
                }else {
                    JavaBean javaBean = new JavaBean(string, Gongutils.getTime());
                    javaBean.setFlag(1);
                    list.add(javaBean);
                    huoQu(string);
                    te_input.setText("");
                    adapter.notifyDataSetChanged();

                }

            }
        });
    }


    /**
     * 设置listView适配器
     */
    private void setClickListener() {
        adapter = new TextAdapter(list,MainActivity.this);
        listView.setAdapter(adapter);
    }

    /**
     * 初始化控件
     */
    private void intiView() {
        mQueue  = Volley.newRequestQueue(MainActivity.this);
        listView = (ListView) findViewById(R.id.listView);
        te_input = (EditText) findViewById(R.id.te_input);
        te_bu = (Button) findViewById(R.id.te_bu);

        list = new ArrayList<>();
        JavaBean bean = new JavaBean("你好", Gongutils.getTime());
        bean.setFlag(2);

        list.add(bean);

    }

    /**
     *
     * @param str 要发送的消息
     */
    private void huoQu(String str) {
       //String url = "http://www.tuling123.com/openapi/api?key=5dbd6db3d89e4776b00840a2601b5240&info=毛主席是谁";
        String url = "http://www.tuling123.com/openapi/api?key=5dbd6db3d89e4776b00840a2601b5240&info=" + str;
        StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    // Toast.makeText(MainActivity.this,response,Toast.LENGTH_LONG).show();
                    int code = jsonObject.optInt("code");
                    if (code == 100000){
                        String text = jsonObject.optString("text");
                        String time = Gongutils.getTime();
                        JavaBean javaBean = new JavaBean(text, time);
                        javaBean.setFlag(2);
                        list.add(javaBean);
                        adapter.notifyDataSetChanged();
                    }else if (code == 200000){
                        String text = jsonObject.optString("text");
                        String url1 = jsonObject.optString("url");
                        Intent intent = new Intent(MainActivity.this, ImageDialog.class);
                        intent.putExtra("text",text);
                        intent.putExtra("url1",url1);
                        startActivity(intent);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(MainActivity.this, error.toString(), Toast.LENGTH_LONG).show();
            }
        });
        mQueue.add(stringRequest);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK){
            dioag();
        }
        return true;
    }

    /**
     * 退出对话框
     */
    private void dioag(){
        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(MainActivity.this);
        normalDialog.setIcon(R.drawable.logo);
        normalDialog.setTitle("提示");
        normalDialog.setMessage("你要丢下我吗？");
        normalDialog.setPositiveButton("是的",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //...To-do
                        dialog.dismiss();
                       finish();
                    }
                });
        normalDialog.setNegativeButton("取消",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //...To-do AlertDialog alertDialog = n
                        dialog.dismiss();
                    }
                });
        // 显示
        normalDialog.show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mQueue = null;
    }
}
